<?php

namespace app\common\model;

use app\admin\model\Command;
use think\Model;

/**
 * 订单类
 */
class Order extends Model
{

    // 开启自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    // 追加属性
    protected $append = [
        'createtime_text',
        'settlement_time_text',
//        'jiesuan_time_text',
        'image_text',
        'baoxiao_msg_text',
        'admin_image_text',
    ];


    public function getImageTextAttr($value, $data)
    {
        if(!empty($data['image'])){
            $value = cdnurl($data['image'],true);
        }
        return $value;
    }
    public function getAdminImageTextAttr($value, $data)
    {
        if(!empty($data['admin_image'])){
            $value = cdnurl($data['admin_image'],true);
        }
        return $value;
    }
    public function getBaoxiaoMsgTextAttr($value, $data)
    {
        if(isset($data['baoxiao_status']) && isset($data['order_status']) && isset($data['jiesuan_time'])  && isset($data['goods_id'])){

            if($data['baoxiao_status'] == 1){
                $xiayue =  date('m', strtotime("+1 month"));
                $nextMonth = $xiayue+1;
                if($nextMonth < 10){
                    $nextMonth = '0'.$nextMonth;
                }

                $jie_day = date('d', $data['jiesuan_time']);
                if($jie_day > 30){
                    $jie_day = 30;
                }
                $packages = Packages::where(['id'=>$data['goods_id']])->find();
                $money    = 0;
                if($packages){
                    $money = round((30 - $jie_day)*($packages['epact_set_meal']/30) + ($packages['ciyue_set_meal'] * 2),2);
                }
                $value  =  $xiayue."月28日前保持在网，".$nextMonth."月10日将为您报销".$money."元";
            }
        }
        return $value;
    }
    public function getCreatetimeTextAttr($value, $data)
    {
        if(isset($data['createtime'])){
            $value = date('Y-m-d H:i:s',$data['createtime']);
        }
        return $value;
    }

    public function getShippingIdcardAttr($value, $data){
        if(isset($data['shipping_idcard'])){
            $value = substr_replace($data['shipping_idcard'] ,"**********",4,10);
        }
        return $value;
    }

    public function getSettlementTimeTextAttr($value, $data)
    {
        if(isset($data['settlement_time'])){
            $value = date('Y-m-d H:i:s',$data['settlement_time']);
        }
        return $value;
    }

    public function getRemarkAttr($value, $data)
    {
        if(isset($data['platform_type']) && !empty($data['order_info_json'])){
            $arr = json_decode($data['order_info_json'],true);
            if($data['platform_type'] == 4){
                $value  = $arr['remark'];
            }
        }
        return $value;
    }

//    public function getJiesuanTimeTextAttr($value, $data)
//    {
//        if(isset($data['goods_id'])){
//
//            $value = date('Y-m-d',settlement_time($data['audit_upload_time']));//二次结算时间;;
//        }
//        return $value;
//    }
    public function packages()
    {
        return $this->belongsTo('app\common\model\Packages', 'goods_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

    public function orderCreateAdd($params,$UserInfo,$info,$orderId){
        $inser['platform_type']      = $info['platform_type'];
        $inser['user_id']            = $UserInfo['id'];
        $inser['parent_id']          = $UserInfo['parent_id'];
        if(isset($params['partnerId'])){
            $inser['partnerId']          = $params['partnerId'];
        }
        $inser['goods_id']           = $info['id'];
        if($info['supplier']){
            $inser['gys']                = $info['supplier'];
        }
        $inser['shipping_contact']   = $params['certName'];//
        $inser['shipping_phone']     = $params['contactNum'];//
        $inser['shipping_idcard']    = $params['certNo'];//
        $inser['shipping_province']  = $params['postProvinceName'];//
        $inser['shipping_city']      = $params['postCityName'];
        $inser['shipping_area']      = $params['postDistrictName'];
        $inser['shipping_address']   = $params['postAddr'];
        if(!empty($params['number'])){
            $inser['phone']              = $params['number'];
        }
        if(!empty($params['outTradeNo'])){
            $inser['outTradeNo']  = $params['outTradeNo'];
        }
        $inser['package_name']       = $info['name'];
        if(!empty($params['agid'])){
            $inser['user_rebate']        = UserPackagesConfig::where(['user_id'=>$params['agid'],'packages_id'=>$info['id']])->value('money')??0;
        }
        $inser['order_id']           = $orderId;
        $inser['post_area'] = $inser['shipping_province'].'-'.$inser['shipping_city'];
        if($inser['shipping_area']){
            $inser['post_area'] .= '-'.$inser['shipping_area'];
        }
        $r = self::create($inser);
        $order_id              = $r->id;
        $inser_two['order_id'] = $order_id;
        $packagesInfo          = $info;
        //顶级代理
        $config                = new UserPackagesConfig();
        $inser_two['goods_id'] = $packagesInfo['id'];
        if (!$UserInfo['parent_id']) {
            $money = $config->where('user_id', $UserInfo['id'])->where(['packages_id' => $packagesInfo['id']])->sum('money');
            if (!$money) {
                $money = $packagesInfo['rebate'];
            }
            $inser_two['user_id'] = $UserInfo['id'];
            $inser_two['money']   = $money;
            $inser_two['level']   = 10;//顶级代理自购
            OrderMore::create($inser_two);
        } else {
            $packages_where['packages_id'] = $packagesInfo['id'];
            if($UserInfo['three_id']){
                $zhi_money = $config->where('user_id', $UserInfo['three_id'])->where($packages_where)->value('money');
                if(!$zhi_money){
                    $zhi_money = $packagesInfo['rebate'];
                }
                $ding_money      = $config->where('user_id', $UserInfo['two_id'])->where($packages_where)->value('money');
                if($ding_money){
                    //上三层
                    $inser_two['user_id'] = $UserInfo['three_id'];
                    $inser_two['money']   = $ding_money;
                    $inser_two['level']   = 9;
                    OrderMore::create($inser_two);
                }else{
                    $ding_money = 0;
                }

                //二层
                $two_money   = $config->where('user_id', $UserInfo['parent_id'])->where($packages_where)->value('money');
                if($two_money){
                    $inser_two['user_id'] = $UserInfo['two_id'];
                    $inser_two['money']   = $two_money;
                    $inser_two['level']   = 8;
                    OrderMore::create($inser_two);
                }else{
                    $two_money = 0;
                }

                //一层
                $parent_money                  = $config->where('user_id', $UserInfo['id'])->where($packages_where)->value('money');
                if($parent_money){
                    $inser_two['user_id']          = $UserInfo['parent_id'];
                    $inser_two['money']            = $parent_money;
                    $inser_two['level']   = 7;//直推
                    OrderMore::create($inser_two);
                }else{
                    $parent_money = 0;
                }
                //自购
                $inser_two['user_id']  = $UserInfo['id'];
                $inser_two['money']    = $zhi_money - ($parent_money + $two_money + $ding_money);
                $inser_two['level']    = 6;
                OrderMore::create($inser_two);

            }elseif ($UserInfo['two_id']){
                //顶级佣金
                $zhi_money = $config->where('user_id', $UserInfo['two_id'])->where($packages_where)->value('money');
                if(!$zhi_money){
                    $zhi_money = $packagesInfo['rebate'];
                }
                //上二层
                $two_money   = $config->where('user_id', $UserInfo['parent_id'])->where($packages_where)->value('money');
                if($two_money){
                    $inser_two['user_id'] = $UserInfo['two_id'];
                    $inser_two['money']   = $two_money;
                    $inser_two['level']   = 8;
                    OrderMore::create($inser_two);
                }else{
                    $two_money = 0;
                }

                //一层
                $parent_money                  = $config->where('user_id', $UserInfo['id'])->where($packages_where)->value('money');
                if($parent_money){
                    $inser_two['user_id']          = $UserInfo['parent_id'];
                    $inser_two['money']            = $parent_money;
                    $inser_two['level']   = 7;//直推
                    OrderMore::create($inser_two);
                }else{
                    $parent_money = 0;
                }
                //自购
                $inser_two['user_id']  = $UserInfo['id'];
                $inser_two['money']    = $zhi_money -  (bcadd($parent_money, $two_money));
                $inser_two['level']    = 6;
                OrderMore::create($inser_two);
            }else{
                //顶级
                $parent_money   = $config->where('user_id', $UserInfo['parent_id'])->where($packages_where)->value('money');
                if(!$parent_money){
                    $parent_money = $packagesInfo['rebate'];
                }
                $one_money                     = $config->where('user_id', $UserInfo['id'])->where($packages_where)->value('money')??0;
                if($one_money){
                    $inser_two['user_id']      = $UserInfo['parent_id'];
                    $inser_two['money']        = $one_money;
                    $inser_two['level']        = 7;//直推
                    OrderMore::create($inser_two);
                }
                //自购
                $inser_two['level']        = 6;//自购
                $inser_two['user_id']      = $UserInfo['id'];
                $inser_two['money']        = $parent_money - $one_money;
                OrderMore::create($inser_two);
            }
        }
    }

    public function orderAddtwo($params,$packagesInfo,$UserInfo){
        $r = self::create($params);
        $order_id              = $r->id;
        $inser_two['order_id'] = $order_id;
        //顶级代理
        $config                = new UserPackagesConfig();
        $inser_two['goods_id'] = $packagesInfo['id'];
        if (!$UserInfo['parent_id']) {
            $money = $config->where('user_id', $UserInfo['id'])->where(['packages_id' => $packagesInfo['id']])->sum('money');
            if (!$money) {
                $money = $packagesInfo['rebate'];
            }
            $inser_two['user_id'] = $UserInfo['id'];
            $inser_two['money']   = $money;
            $inser_two['level']   = 10;//顶级代理自购
            OrderMore::create($inser_two);
        } else {
            $packages_where['packages_id'] = $packagesInfo['id'];
            if($UserInfo['three_id']){
                $zhi_money = $config->where('user_id', $UserInfo['three_id'])->where($packages_where)->value('money');
                if(!$zhi_money){
                    $zhi_money = $packagesInfo['rebate'];
                }
                $ding_money      = $config->where('user_id', $UserInfo['two_id'])->where($packages_where)->value('money');
                if($ding_money){
                    //上三层
                    $inser_two['user_id'] = $UserInfo['three_id'];
                    $inser_two['money']   = $ding_money;
                    $inser_two['level']   = 9;
                    OrderMore::create($inser_two);
                }else{
                    $ding_money = 0;
                }

                //二层
                $two_money   = $config->where('user_id', $UserInfo['parent_id'])->where($packages_where)->value('money');
                if($two_money){
                    $inser_two['user_id'] = $UserInfo['two_id'];
                    $inser_two['money']   = $two_money;
                    $inser_two['level']   = 8;
                    OrderMore::create($inser_two);
                }else{
                    $two_money = 0;
                }

                //一层
                $parent_money                  = $config->where('user_id', $UserInfo['id'])->where($packages_where)->value('money');
                if($parent_money){
                    $inser_two['user_id']          = $UserInfo['parent_id'];
                    $inser_two['money']            = $parent_money;
                    $inser_two['level']   = 7;//直推
                    OrderMore::create($inser_two);
                }else{
                    $parent_money = 0;
                }
                //自购
                $inser_two['user_id']  = $UserInfo['id'];
                $inser_two['money']    = $zhi_money - ($parent_money + $two_money + $ding_money);
                $inser_two['level']    = 6;
                OrderMore::create($inser_two);

            }elseif ($UserInfo['two_id']){
                //顶级佣金
                $zhi_money = $config->where('user_id', $UserInfo['two_id'])->where($packages_where)->value('money');
                if(!$zhi_money){
                    $zhi_money = $packagesInfo['rebate'];
                }
                //上二层
                $two_money   = $config->where('user_id', $UserInfo['parent_id'])->where($packages_where)->value('money');
                if($two_money){
                    $inser_two['user_id'] = $UserInfo['two_id'];
                    $inser_two['money']   = $two_money;
                    $inser_two['level']   = 8;
                    OrderMore::create($inser_two);
                }else{
                    $two_money = 0;
                }

                //一层
                $parent_money                  = $config->where('user_id', $UserInfo['id'])->where($packages_where)->value('money');
                if($parent_money){
                    $inser_two['user_id']          = $UserInfo['parent_id'];
                    $inser_two['money']            = $parent_money;
                    $inser_two['level']   = 7;//直推
                    OrderMore::create($inser_two);
                }else{
                    $parent_money = 0;
                }
                //自购
                $inser_two['user_id']  = $UserInfo['id'];
                $inser_two['money']    = $zhi_money -  (bcadd($parent_money, $two_money));
                $inser_two['level']    = 6;
                OrderMore::create($inser_two);
            }else{
                //顶级
                $parent_money   = $config->where('user_id', $UserInfo['parent_id'])->where($packages_where)->value('money');
                if(!$parent_money){
                    $parent_money = $packagesInfo['rebate'];
                }
                $one_money                     = $config->where('user_id', $UserInfo['id'])->where($packages_where)->value('money')??0;
                if($one_money){
                    $inser_two['user_id']      = $UserInfo['parent_id'];
                    $inser_two['money']        = $one_money;
                    $inser_two['level']        = 7;//直推
                    OrderMore::create($inser_two);
                }
                //自购
                $inser_two['level']        = 6;//自购
                $inser_two['user_id']      = $UserInfo['id'];
                $inser_two['money']        = $parent_money - $one_money;
                OrderMore::create($inser_two);
            }
        }
    }
}

